<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class message extends core {

	public function __construct() {
		parent::__construct();
	}

	//消息
	public function index() {
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		
		//by ajsong 20151102 如果没有登录即返回空数据
		if ($this->member_id<=0) success('');
		
		$sql = "SELECT * FROM jf_message
			WHERE member_id='{$this->member_id}' ORDER BY id DESC LIMIT {$offset}, {$pagesize}";
		//$sql = "SELECT * FROM jf_message
		//	WHERE member_id='0' ORDER BY id DESC LIMIT {$offset}, {$pagesize}";
		//echo $sql;
		$messages = $this->db->get_results($sql);
		if ($messages) {
			foreach ($messages as $k=>$message) {
				//$messages[$k]->add_time = get_time_word($message->add_time);
				$messages[$k]->add_time = date("Y-m-d H:i:s", $message->add_time);
			}
		}
		//$sql = "UPDATE xfb_message SET readed=1 WHERE member_id='{$this->member_id}'";
		//$this->db->query($sql);
		success($messages);
	}

	//阅读消息
	public function read() {
		$id = (isset($_POST['id']) && trim($_POST['id'])) ? intval($_POST['id']) : 0;
		$sql = "UPDATE jf_message SET readed=1 WHERE member_id='{$this->member_id}' AND id='{$id}'";
		$this->db->query($sql);
		success("ok");
	}

	//删除消息
	public function delete() {
		$id = (isset($_POST['id']) && trim($_POST['id'])) ? intval($_POST['id']) : 0;
		$sql = "DELETE FROM jf_message WHERE id='{$id}' AND member_id='{$this->member_id}'";
		//error($sql);
		$this->db->query($sql);
		success("ok");
	}

}
